import json
import os
import sys

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + "../")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hf_mall.settings")

import django

django.setup()
from xpinyin import Pinyin
from goods.models import GoodsCategory

p = Pinyin()
with open('./json/4/categoryList.json', 'r', encoding='utf8')as fp:
    row_data = json.load(fp)

for lev1_cat in row_data:
    lev1_intance = GoodsCategory()
    lev1_intance.category_code = lev1_cat['category_code']
    lev1_intance.category_name = lev1_cat['category_name']
    lev1_intance.category_level = 1
    lev1_intance.save()

    for lev2_cat in lev1_cat['category_sub']:
        lev2_intance = GoodsCategory()
        lev2_intance.category_code = lev2_cat['category_code']
        lev2_intance.category_name = lev2_cat['category_name']
        lev2_intance.category_level = 2
        lev2_intance.parent_category = lev1_intance
        lev2_intance.save()

        for lev3_cat in lev2_cat['category_sub']:
            lev3_intance = GoodsCategory()
            lev3_intance.category_code = lev3_cat['category_code']
            lev3_intance.category_name = lev3_cat['category_name']
            lev3_intance.category_img = lev3_cat['category_img']
            lev3_intance.category_level = 3
            lev3_intance.parent_category = lev2_intance
            lev3_intance.save()
